<?php
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
class Create_Video_Xml
{
	var $str_xml = '';
	//创建ws网站的视频xml
	public function ws_xml()
	{
		$xml_file = '/var/www/website/cms.wondershare.com.br/httpdocs/vhosts/wondershare.com/httpdocs/';
		//ws产品管理模板
		$this->str_xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"  xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> '.chr(10);
		$sql = "SELECT p.product_video_promo,won_p.top_image,tab.url,tab.Field_20046,
		tab.Field_20048 FROM   Table_26  tab LEFT JOIN won_product won_p ON tab.Field_20044 = won_p.id
		LEFT JOIN product  p ON won_p.won_product_id = p.product_id WHERE p.product_video_promo != '' ";
		$result = $GLOBALS['db']->query($sql);
		
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			$this->str_xml .= $this->create_element($row['url'],$row['top_image'],$row['Field_20046'],$row['Field_20048']
			,$row['product_video_promo'],'',1);
		
		}
		
		//ws产品单页管理模板
		$sql = "SELECT tab.url,tab.Field_20355, tab.Field_20357 ,tab.Field_20361 FROM   Table_45  tab LEFT JOIN 
		won_product won_p ON tab.Field_20350 = won_p.id LEFT JOIN product  p ON won_p.won_product_id = p.product_id 
		WHERE  tab.Field_20352 LIKE  '%http://www.youtube.com%' AND tab.Field_20361 = 1  AND !FIND_IN_SET(won_p.id,'86,64')   ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<div class="pic">(\s*?)<a class="various3"(\s*?)href="(.*?)">(.*?)<div class="video">/si',
			$row['Field_20352'], $data);
			$image_url = $this->get_image_url($row['Field_20352'],1,'http://www.wondershare.com/');
			if(!empty($data[3]))
			{
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_20357'],$row['Field_20361']
				,$data[3],'youtobe',1);
			}
		}
		
		//ws文章1模板
		$sql = "SELECT tab.url,tab.Field_19964, tab.Field_19915,ar.ar_content   FROM   Table_13  tab 
		LEFT JOIN  topic_article ar   ON tab.Field_19965 = ar.ar_id WHERE  ar.ar_content LIKE  '%http://www.youtube.com%'
		AND ar.ar_content LIKE  '%</embed>%' ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<embed(.*?)src="(.*?)"(.*?)>(.*?)<\/embed>/si',$row['ar_content'], $data);
			if(!empty($data[2]) && (substr_count($data[2],'http://www.youtube.com') > 0) )
			{
				$image_url = $this->get_image_url($row['ar_content'],1,'http://www.wondershare.com/');
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_19964'],$row['Field_19915']
				,$data[2],'youtobe',1);
			}
		}
		//ws文章2模板
		$sql = "SELECT tab.url,tab.Field_20162, tab.Field_20164,ar.ar_content   FROM   Table_31  tab 
		LEFT JOIN  topic_article ar   ON tab.Field_20160 = ar.ar_id WHERE  ar.ar_content LIKE  '%http://www.youtube.com%'
		AND ar.ar_content LIKE  '%</embed>%' ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<embed(.*?)src="(.*?)"(.*?)>(.*?)<\/embed>/si',$row['ar_content'], $data);
			if(!empty($data[2]) && (substr_count($data[2],'http://www.youtube.com') > 0) )
			{
				$image_url = $this->get_image_url($row['ar_content'],1,'http://www.wondershare.com/');
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_20162'],$row['Field_20164']
				,$data[2],'youtobe',1);
			}
		}
		$this->str_xml .= '</urlset>';
		
		//以下是新建一个文件
		$this->create_file($xml_file,'video-sitemap.xml',$this->str_xml,1);
		echo  '<script type="text/javascript"> 
		alert("生成video-sitemap.xml成功！");
		window.location.href="http://cms.wondershare.com.br.wx/wondershare/article.php?action=index&w_type=1";
		</script>';
	}
	//创建IS网站的视频xml
	public function is_xml()
	{
		$xml_file = '/var/www/website/cms.wondershare.com.br/httpdocs/vhosts/iskysoft.com/httpdocs/';
		$this->str_xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"  xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> '.chr(10);
		
		//IS文章模板
		$sql = "SELECT tab.url,tab.Field_20684, tab.Field_20687,ar.ar_content   FROM   Table_71  tab 
		LEFT JOIN  topic_article ar   ON tab.Field_20681 = ar.ar_id WHERE  ar.ar_content LIKE  '%http://www.youtube.com%'
		AND ar.ar_content LIKE  '%</embed>%' ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<embed(.*?)src="(.*?)"(.*?)>(.*?)<\/embed>/si',$row['ar_content'], $data);
			if(!empty($data[2]) && (substr_count($data[2],'http://www.youtube.com') > 0) )
			{
				$image_url = $this->get_image_url($row['ar_content'],8,'http://www.iskysoft.com/');
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_20684'],$row['Field_20687']
				,$data[2],'youtobe',8);
			}
		}
		$this->str_xml .= '</urlset>';
		
		//以下是新建一个文件
		$this->create_file($xml_file,'articlevideo-sitemap.xml',$this->str_xml,8);
		echo  '<script type="text/javascript"> 
		alert("生成articlevideo-sitemap.xml成功！");
		window.location.href="http://cms.wondershare.com.br.wx/wondershare/article.php?action=index&w_type=8";
		</script>';
	}

	//创建Aimer网站的视频xml
	public function am_xml()
	{
		$xml_file = '/var/www/website/cms.wondershare.com.br/httpdocs/vhosts/aimersoft.com/httpdocs/';
		$this->str_xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"  xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> '.chr(10);
		
		//am文章模板
		$sql = "SELECT tab.url,tab.Field_20533, tab.Field_20535,ar.ar_content   FROM   Table_59  tab 
		LEFT JOIN  topic_article ar   ON tab.Field_20531 = ar.ar_id WHERE  ar.ar_content LIKE  '%http://www.youtube.com%'
		AND ar.ar_content LIKE  '%</embed>%' ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<embed(.*?)src="(.*?)"(.*?)>(.*?)<\/embed>/si',$row['ar_content'], $data);
			if(!empty($data[2]) && (substr_count($data[2],'http://www.youtube.com') > 0) )
			{
				$image_url = $this->get_image_url($row['ar_content'],2,'http://www.aimersoft.com/');
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_20533'],$row['Field_20535']
				,$data[2],'youtobe',2);
			}
		}
		$this->str_xml .= '</urlset>';
		
		//以下是新建一个文件
		$this->create_file($xml_file,'articlevideo-sitemap.xml',$this->str_xml,2);
		echo  '<script type="text/javascript"> 
		alert("生成articlevideo-sitemap.xml成功！");
		window.location.href="http://cms.wondershare.com.br.wx/wondershare/article.php?action=index&w_type=2";
		</script>';
	}
	//创建applemacvideo.com网站的视频xml
	public function app_xml()
	{
		$xml_file = '/var/www/website/cms.wondershare.com.br/httpdocs/vhosts/applemacvideo.com/httpdocs/';
		$this->str_xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"  xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> '.chr(10);
		
		//am文章模板
		$sql = "SELECT tab.url,tab.Field_20430, tab.Field_20432,ar.ar_content   FROM   Table_51  tab 
		LEFT JOIN  topic_article ar   ON tab.Field_20428 = ar.ar_id WHERE  ar.ar_content LIKE  '%http://www.youtube.com%'
		AND ar.ar_content LIKE  '%</embed>%' ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			preg_match('/<embed(.*?)src="(.*?)"(.*?)>(.*?)<\/embed>/si',$row['ar_content'], $data);
			if(!empty($data[2]) && (substr_count($data[2],'http://www.youtube.com') > 0) )
			{
				$image_url = $this->get_image_url($row['ar_content'],4,'http://www.applemacvideo.com/');
				$this->str_xml .= $this->create_element($row['url'],$image_url,$row['Field_20430'],$row['Field_20432']
				,$data[2],'youtobe',4);
			}
		}
		$this->str_xml .= '</urlset>';
		
		//以下是新建一个文件
		$this->create_file($xml_file,'articlevideo-sitemap.xml',$this->str_xml,4);
		echo  '<script type="text/javascript"> 
		alert("生成articlevideo-sitemap.xml成功！");
		window.location.href="http://cms.wondershare.com.br.wx/wondershare/article.php?action=index&w_type=4";
		</script>';
	}
	//新建一个文件
	function  create_file($path_file,$file_name,$data,$web_type = 0)
	{
		if (is_writable($path_file) == false) 
		{
			echo  '<script type="text/javascript"> 
			alert("木有新建权限！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/article.php?action=index&w_type="'.$web_type.';
			</script>';
		}
		else 
		{
			$path_file = $path_file.$file_name;
			if(file_exists($path_file))
			{
				unlink($path_file);
			}
			file_put_contents($path_file, $data);
		}
	}
	//创建xml元素
	function create_element($url,$thumbnail_loc,$title,$description,$content_loc,$type = 'youtobe',$web_type = 0)
	{
		$url = str_replace('index.html','',$url);
		$title = str_replace('&','and',$title);
		$description = str_replace('&','and',$description);
		if($type == 'youtobe')
		{
			$new_content_loc = str_replace('&amp;','&',$content_loc);
			$new_content_loc = substr($content_loc,0,strpos($new_content_loc,'&'));
			if(empty($new_content_loc))
			{
				$new_content_loc = $content_loc;
			}
		}else
		{
			$new_content_loc = $content_loc;
		}
		$element_xml = '';
		if($web_type == 1)//ws网站
		{
			$url = str_replace('/wondershare.com/httpdocs/','http://www.wondershare.com/',$url);
		}elseif($web_type == 2)//am网站
		{
			$url = str_replace('/aimersoft.com/httpdocs/','http://www.aimersoft.com/',$url);
		}elseif($web_type == 8)//is网站
		{
			$url = str_replace('/iskysoft.com/httpdocs/','http://www.iskysoft.com/',$url);
		}elseif($web_type == 4)//app网站
		{
			$url = str_replace('/applemacvideo.com/httpdocs/','http://www.applemacvideo.com/',$url);
		}
		$element_xml.='<url>'.chr(10).'<loc>'.$url.'</loc>'.chr(10).'<video:video>'.chr(10);
		$element_xml.='<video:thumbnail_loc>'.$thumbnail_loc.'</video:thumbnail_loc>'.chr(10);
		$element_xml.='<video:title>'.$title.'</video:title>'.chr(10);
		$element_xml.='<video:description>'.$description.'</video:description>'.chr(10);
		$element_xml.='<video:content_loc>'.$new_content_loc.'</video:content_loc>'.chr(10);
		$element_xml.='</video:video>'.chr(10).'</url>'.chr(10);
		return $element_xml;
	}
	//得到大于100像数的图片
	function get_image_url($ar_content,$web_type,$web_index)
	{
		$image_url = '';
		preg_match_all('/<img(.*?)src="(.*?)"(.*?)>/si',$ar_content, $data);
		$nums = count($data[2]);
		for($j = 0;$j < $nums;$j++)
		{
			$data[2][$j] = str_replace('../../',$web_index,$data[2][$j]);
			$data[2][$j] = str_replace('../',$web_index,$data[2][$j]);
			$image_info = getimagesize($data[2][$j]);
			if($image_info[1] >= 100) //高度的像素数 大于100
			{
				$image_url = $data[2][$j];
				break;
			}
			if($j+1 == $nums )
			{
				$image_url = $data[2][$j];
			}
		
		}
		if(empty($image_url))
		{
			if($web_type == 1)
			{
				$image_url = 'http://images.wondershare.com/style/style_img/wondershare_logo.png';
			}elseif($web_type == 4)//app网站
			{
				$image_url = 'http://www.applemacvideo.com/style/images/logo.png';
			}elseif($web_type == 8)//is网站
			{
				$image_url = 'http://www.iskysoft.com/images/style/logo-top.jpg';
			}elseif($web_type == 2)//am网站
			{
				$image_url = 'http://www.aimersoft.com/style/images/logo.png';
			}
		}
		return $image_url;
	}
}

$action    = $_REQUEST['action']; 
$video_xml  = new Create_Video_Xml;
if(method_exists($video_xml, $action)) 
{
	$video_xml->$action();
}else{
	$video_xml->index();
}



?>